package patient

import (
	"api-gateway/basic/config"
	proto "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"context"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"time"
)

// GetInfo 获取患者信息
func GetInfo(c *gin.Context) {
	userID := c.GetInt64("userId")
	if userID == 0 {
		c.JSON(http.StatusOK, gin.H{"code": 401, "msg": "用户未登录"})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.GetPatientInfo(ctx, &proto.GetPatientInfoRequest{
		UserId: userID,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "获取患者信息失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
		"data": resp.Patient,
	})
}

// Verify 患者实名认证
func Verify(c *gin.Context) {
	userID := c.GetInt64("userId")
	if userID == 0 {
		c.JSON(http.StatusOK, gin.H{"code": 401, "msg": "用户未登录"})
		return
	}

	var form request.PatientVerifyReq
	if err := c.ShouldBindJSON(&form); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "参数错误"})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.VerifyPatient(ctx, &proto.VerifyPatientRequest{
		UserId:   userID,
		RealName: form.RealName,
		IdCard:   form.IdCard,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "实名认证失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
	})
}

// Update 更新患者档案
func Update(c *gin.Context) {
	userID := c.GetInt64("userId")
	if userID == 0 {
		c.JSON(http.StatusOK, gin.H{"code": 401, "msg": "用户未登录"})
		return
	}

	var form request.UpdatePatientReq
	if err := c.ShouldBindJSON(&form); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "参数错误"})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.UpdatePatientInfo(ctx, &proto.UpdatePatientInfoRequest{
		UserId:    userID,
		Gender:    int32(form.Gender),
		BirthDate: form.BirthDate,
		Phone:     form.Phone,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "更新患者信息失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
	})
}

// GetList 患者列表（就诊人列表）
func GetList(c *gin.Context) {
	userID := c.GetInt64("userId")
	if userID == 0 {
		c.JSON(http.StatusOK, gin.H{"code": 401, "msg": "用户未登录"})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.GetPatientList(ctx, &proto.GetPatientListRequest{
		UserId: userID,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "获取患者列表失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
		"data": resp.Patients,
	})
}

// Add 新增患者（就诊人）
func Add(c *gin.Context) {
	userID := c.GetInt64("userId")
	if userID == 0 {
		c.JSON(http.StatusOK, gin.H{"code": 401, "msg": "用户未登录"})
		return
	}

	var form request.AddPatientReq
	if err := c.ShouldBindJSON(&form); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "参数错误"})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.AddPatient(ctx, &proto.AddPatientRequest{
		UserId:    userID,
		Name:      form.Name,
		Gender:    int32(form.Gender),
		BirthDate: form.BirthDate,
		Phone:     form.Phone,
		IdCard:    form.IdCard,
		Relation:  form.Relation,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "添加患者失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
	})
}

// Delete 删除患者
func Delete(c *gin.Context) {
	userID := c.GetInt64("userId")
	if userID == 0 {
		c.JSON(http.StatusOK, gin.H{"code": 401, "msg": "用户未登录"})
		return
	}

	patientIdStr := c.Param("id")
	patientId, err := strconv.ParseInt(patientIdStr, 10, 64)
	if err != nil || patientId <= 0 {
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "患者ID格式错误"})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.DeletePatient(ctx, &proto.DeletePatientRequest{
		UserId:    userID,
		PatientId: patientId,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "删除患者失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
	})
}
